package wx.controls
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	
	/**
	 * 
	 * @author wujianqi
	 * 
	 * 嵌套式容器控件 基类 
	 * 
	 */	
	internal class ContainerBase extends Sprite
	{
		
		/**
		 * 修饰层 
		 */		
		protected var overlay:DisplayObjectContainer;
		
		/**
		 * 容器层 
		 */		
		protected var contentGroup:DisplayObjectContainer;
		
		public function ContainerBase($overlay:DisplayObjectContainer=null,$contentGroup:DisplayObjectContainer=null)
		{
			super();
			overlay = $overlay;
			contentGroup = $contentGroup;
			if(overlay!=null)  updateOverlay();
			if(contentGroup!=null)  updateContent();
			
			addEventListener(Event.ADDED_TO_STAGE,onLoaded);
		}
		
		/**
		 *  更新修饰层
		 */		
		protected function updateOverlay():void{
			if(overlay!=null && super.contains(overlay)) 
				super.removeChild(overlay);
			
			if(overlay!=null) super.addChild(overlay);
		}
		
		/**
		 * 
		 * 更新内容层
		 */		
		protected function updateContent():void{
			if(contentGroup!=null && super.contains(contentGroup)) 
				super.removeChild(contentGroup);
			
			if(contentGroup!=null) super.addChild(contentGroup);
		}
		
		
		override public function addChild(child:DisplayObject):DisplayObject{			
			return  contentGroup.addChild(child);
		}
		override public function addChildAt(child:DisplayObject, index:int):DisplayObject{
			return contentGroup.addChildAt(child,index);;
		}
		override public function setChildIndex(child:DisplayObject, index:int):void{
			contentGroup.setChildIndex(child,index);
		}
		override public function get numChildren():int{
			return contentGroup.numChildren;
		}
		override public function getChildAt(index:int):DisplayObject{
			return contentGroup.getChildAt(index);
		}
		override public function getChildIndex(child:DisplayObject):int{
			return contentGroup.getChildIndex(child);
		}
		override public function getChildByName(name:String):DisplayObject{
			return contentGroup.getChildByName(name);
		}
		override public function removeChild(child:DisplayObject):DisplayObject{
			return contentGroup.removeChild(child);
		}
		override public function removeChildAt(index:int):DisplayObject{
			return contentGroup.removeChildAt(index);
		}
		override public function removeChildren(beginIndex:int=0, endIndex:int=2147483647):void{
			return contentGroup.removeChildren(beginIndex,endIndex);
		}
		override public function swapChildren(child1:DisplayObject, child2:DisplayObject):void{
			contentGroup.swapChildren(child1,child2);
		}
		override public function swapChildrenAt(index1:int, index2:int):void{
			contentGroup.swapChildrenAt(index1,index2);
		} 
		override public function contains(child:DisplayObject):Boolean{
			return contentGroup.contains(child);
		} 
		
		
		/**
		 * 组件移除gc处理
		 */	
		protected function dispose():void {}
		private function onLoaded(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE,onLoaded);			
			addEventListener(Event.REMOVED_FROM_STAGE,function():void{				
				removeEventListener(Event.REMOVED_FROM_STAGE,arguments.callee);
				dispose();
			});
		}
		
		
	}
}